package com.hxut.mrs.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * description: WebSocket服务器端推送消息示例Controller
 * date: 2023/4/8 8:45
 * author: MR.孙
 */
@RestController
@RequestMapping("/socket")
public class WebSocketController {

    /**
     * 群发消息内容
     * @param message
     * @return
     */
    @GetMapping("/sendAll")
    public String sendAllMessage(@RequestParam(required = true) String message) {

        try {
            WebSocketServer.broadCastInfo(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }


    /**
     * 指定会话ID发消息
     * @param message
     * @param id
     * @return
     */
    @GetMapping("/sendOne")
    public String sendOneMessage(@RequestParam(required = true) String message, @RequestParam(required=true) String id) {

        try {
            WebSocketServer.sendMessage(id, message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return "ok";

    }

}
